package com.coolw.dynamic.thread.pool.enums;

import com.coolw.common.exception.ExceptionUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.text.StrUtil;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * 线程存活时间单位定义
 *
 * @author coolw
 * @date 2024/5/16 10:17
 */
@Getter
@AllArgsConstructor
public enum KeepAliveTimeUnitEnum {

    SECOND("SECOND", "秒", TimeUnit.SECONDS),
    MINUTE("MINUTE", "分", TimeUnit.MINUTES),
    HOUR("HOUR", "时", TimeUnit.HOURS),
    DAY("DAY", "天", TimeUnit.DAYS);

    private final String code;

    private final String message;

    private final TimeUnit timeUnit;

    public static KeepAliveTimeUnitEnum getEnum(String code) {
        return Arrays.stream(values())
                .filter(r -> StrUtil.equals(code, r.getCode()))
                .findFirst()
                .orElse(null);
    }

    public static TimeUnit getTimeUnit(String code) {
        KeepAliveTimeUnitEnum timeUnitEnum = getEnum(code);

        Assert.notNull(timeUnitEnum, () ->
                ExceptionUtil.createBusinessException(StrUtil.format("线程池存活时间单位[{}]未定义", code)));

        return timeUnitEnum.getTimeUnit();
    }
}
